<!DOCTYPE html>
<html class="index_body">
<head lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=10">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <title></title>
    <link rel="stylesheet" href="/Public/mui/computer/css/bootstrap.css"/>
    <link rel="stylesheet" href="/Public/mui/computer/css/index.css"/>
    <link rel="stylesheet" href="/Public/mui/css/page.css"/>
    <link rel="stylesheet"  href="/Public/mui/static/bower_components/sweetalert/dist/sweetalert.css">

</head>
<body class="index_body">
<include file="Lay:heder" />
<div id="section" style="padding-top: 20px">
    <div class="add_cangku">
        <a href="" id="imgmodeltanchu">新增仓库</a>
    </div>
    <table class="table table-striped table-hover all_table">
        <thead class="all_thead">
            <tr>
               <th>库存所在地</th>
               <th>颜色</th>
               <th>库存数量</th>
               <th>操作</th>
            </tr>
        </thead>
        <tbody>
        <!-- <?php print_r($result);?> -->
          <foreach name="result" item="v" key="k">
             <tr orderid="{{$v['id']}}">
              <td class="cityname">{{$v['names']}}/{{$v['name']}}</td>
              <td>
                  <select name="car_color" class="onegcolor">
                    <foreach name="v['color']" item="vo" key="ko">
                        <option value="{{$vo['id']}}" {{$ko == "0" ? "selected":""}}>{{$vo['color']}}</option>
                    </foreach>
                  </select>
                  <input type="text" class="input_edit" style='border: 1px solid #ccc;width:7%;display: none;'>   
                  <button type="button" class="butt buttlt">编辑</button>
              </td>
              <td class="car_number">
                  <!-- <span class="car_number1">{{$v['color'][0]['stock']}}</span> -->
                  <input type='number' class="input_stock" value="" style='border: 1px solid #ccc;margin: 0 auto;'>
              </td>
              <td>
                  <button type="button" class="buttonss buttlt" kj="{{$k}}">保存</button>
                  <button type="button" class="buttdel" style="color:red;" kj="{{$v['id']}}">删除</button>
              </td>
            </tr>
         </foreach>
        </tbody>
    </table>
    <include file="News:news" />
    <!-- <input type="hidden" class="valresult" value='<?= json_encode($result,true);?>' /> -->
</div>
<script src="/Public/mui/computer/js/jquery-1.11.3.js"></script>
<script src="/Public/mui/computer/js/sotck.js"></script>
<script type="text/javascript" src="/Public/mui/static/bower_components/sweetalert/dist/sweetalert.min.js"></script>
<script type="text/javascript" src="/Public/mui/computer/js/functions.js?v=asdf2"></script>
<script src="/Public/mui/computer/js/back.js"></script>
<script>
  $("#logo_type").attr("src","/Public/mui/computer/img/kucun.png");
  var carid =  GetQueryString('carnum');
  document.getElementById("imgmodeltanchu").href='add_cangku.html?carnum='+carid;
   // $("tr").delegate(".car_number","click",function(){
   //  if($(this).find("input").length && $(this).find("input").length>0){
   //       $(this).children("span").hide();
   //       $(this).find(".sstock").css("display","block");
   //       $("input").focus();
   //     }else{
   //      $(this).children("span").hide();
   //      $(this).append("<input type='number' class='sstock' style='border: 1px solid #ccc;margin: 0 auto;'>");
   //       $("input").focus();
   //     }
   // })
   // $("tr").delegate(".car_number","blur",function(){
   //    var bock = $(this).find("input").val();
   //    if(bock ==""){
   //      console.log(bock)
   //       $(this).children("span").show();
   //      $(this).find(".sstock").css("display","none");
   //    }
   // })
   $(function(){
    eval('var paddstock = (<?php echo json_encode($result)?>)');
    
    $("tbody tr").each(function(){
        index =  $(this).index();
        typindex =paddstock[index]['color'][0]['stock'];
        $(this).find('.input_stock').val(typindex);
    });
   });
   $('tbody tr').click(  
         function() {  
              index =  $(this).index();
              eval('var padding = (<?php echo json_encode($result)?>)');
              $(".onegcolor").change(function(){
                typindex =  $(this).get(0).selectedIndex
                typindex = padding[index]['color'][typindex]['stock'];
                $(this).parents('tr').find('.input_stock').val(typindex);
              }); 
          }
      );

  //删除库存
  $('.buttdel').each(function(){
      $(this).click(function(){
          index =  $(this).attr("kj");
          if(!index){
              sweetAlert('','对不起!未获取到城市ID,请刷新重试!','error');
              return false;
           }
           data = {id:index,carnum:carid};
          var cityname   =  $(this).parents('tr').find('.cityname').text();
          swal({ 
                title: "确定删除"+cityname+"的库存吗？", 
                text: "你将删除"+cityname+"的库存....！", 
                type: "warning",
                showCancelButton: true, 
                confirmButtonColor: "#DD6B55",
                cancelButtonText: "取消",
                confirmButtonText: "删除！", 
                closeOnConfirm: false
              },
            function(){
              post("/admin/Stock/deletecangku",data,function(res){
                console.log(res)
                if(res.success == "OK"){
                    sweetAlert('',res.msg,'success');
                    setTimeout(function () {
                              location.href = '/admin/Stock/cangku?carnum='+carid;
                            },2000);
                }else if(res.success == "ERROR"){
                  sweetAlert('',res.msg,'success');
                    return false;
                }
              });
            });
      });
  });
  //修改库存
  $('.buttonss').each(function() {  
        $(this).click(function(){
             index =  $(this).attr("kj");
             var valstock   =  $(this).parents('tr').find('.input_stock').val();
             var colorstock =  $(this).parents('tr').find('.onegcolor').val();
             if(!valstock){
                    sweetAlert('','对不起!您未做任何操作!','error');
                    return false;
             }
             data = {id:colorstock,stock:valstock};
             swal({ 
                title: "确定修改库存吗？", 
                text: "你将修改库存....！", 
                type: "warning",
                showCancelButton: true, 
                confirmButtonColor: "#DD6B55",
                cancelButtonText: "取消",
                confirmButtonText: "确定修改！", 
                closeOnConfirm: false
              },
              function(){
                  editstock(data);
              });
        })
    }
  );
  //修改库存
  function editstock(data){
      post("/admin/Stock/editcangku",data,function(res){
        if(res.ret="OK"){
          sweetAlert('',res.msg,'success');
          setTimeout(function () {
                  location.href = '/admin/Stock/cangku?carnum='+carid;
                },1000);
        }else if(res.ret="ERROR"){
            sweetAlert('',res.msg,'error');
            return false;
        }else{
            sweetAlert('',res.msg,'error');
            return false;
        }
        console.log(res);
      });
  }
  function GetQueryString(name)
  {
       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
       var r = window.location.search.substr(1).match(reg);
       if(r!=null)return  unescape(r[2]); return null;
  }
  $('.input_edit').each(function(){
      $(this).blur(function(){
          var color = $(this).val();
           var colorstock =  $(this).parents('tr').find('.onegcolor').val();
           var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
           if(!reg.test(color)){
                sweetAlert('','对不起!请输入正确的颜色','error');
                return false;
           }
           data = {color:color,stockid:colorstock};
           post("/admin/Stock/eidtstock",data,function(res){
            console.log(res);
              if(res.success == "OK"){
                  setTimeout(function () {
                              location.href = '/admin/Stock/cangku?carnum='+carid;
                            },1000);
              }else{
                 setTimeout(function () {
                              location.href = '/admin/Stock/cangku?carnum='+carid;
                            },1000);
              }
           });
      });
  });
  //修改颜色
  
  $(".butt").on("click",function(){
     $(this).siblings("input").show();
     console.log(12123);
     $(this).hide();

  });

</script>
</body>
</html>